package com.jash.designpack2;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;


/**
 * A simple {@link Fragment} subclass.
 */
public class PagerFragment extends Fragment implements View.OnFocusChangeListener, TextWatcher {


    private TextInputLayout textInput;

    public static PagerFragment getInstance(String data) {
        PagerFragment fragment = new PagerFragment();
        Bundle bundle = new Bundle();
        bundle.putString("data", data);
        fragment.setArguments(bundle);
        return fragment;
    }
    public PagerFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_pager, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        TextView text = (TextView) view.findViewById(R.id.pager_text);
        String data = getArguments().getString("data");
        text.setText(data);
        textInput = ((TextInputLayout) view.findViewById(R.id.text_input));
//        textInput.getEditText().setOnFocusChangeListener(this);
        textInput.getEditText().addTextChangedListener(this);
    }

    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        EditText editText = (EditText) view;
        if (!hasFocus) {
            if (editText.getText().length() >= 6) {
                textInput.setErrorEnabled(false);
            } else {
                textInput.setError("必须在6位以上");
                textInput.setErrorEnabled(true);
            }
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        if (editable.length() >= 6) {
            textInput.setErrorEnabled(false);
        } else {
            textInput.setError("必须在6位以上");
            textInput.setErrorEnabled(true);
        }
    }
}
